package com.ym.b2b.provider.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.ym.b2b.commons.exception.BaseException;
import com.ym.b2b.commons.service.BaseService;
import com.ym.b2b.commons.util.CheckData;
import com.ym.b2b.provider.dao.EmCProviderDAO;
import com.ym.b2b.provider.service.EmCProviderService;
import com.ym.b2b.provider.sqlbean.EmCProvider;
import com.ym.b2b.user.dao.EmCUserDAO;
import com.ym.b2b.user.sqlbean.EmCUser;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;

/**
 * @ClassName: EmCProviderServiceImpl
 * @Description: TODO
 * @Author tangxj
 * @Date 2015-1-15 上午11:19:09
 * @Modify
 * @CopyRight B2B小组
 */

@Service
public class EmCProviderServiceImpl extends BaseService implements EmCProviderService {

    private Logger         logger = Logger.getLogger(getClass());
    @Autowired
    private EmCProviderDAO emCProviderDAO;

    @Autowired
    private EmCUserDAO     userdao;
    
    private Logger         dbLog = Logger.getLogger("db");

    /*
     * 分页查询
     */
    @Override
    public Result searchPageEmCProvider(Page<EmCProvider> page, EmCProvider emCProvider) throws DataAccessException {
        Result result = new Result();

        try {
            String areaCode = emCProvider.getAreaCode();
            if (areaCode != null && !"".equals(areaCode)) {
                CheckData cd = new CheckData();
                areaCode = cd.checkSearchAreaCode(areaCode);
                emCProvider.setAreaCode(areaCode);
            }
            result.setResultPage(emCProviderDAO.searchPageEmCProvider(emCProvider, page));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "查询异常");
        }
        return result;
    }

    /* 
     *
     */
    @Override
    public Result showEmCProviderDetail(EmCProvider emCProvider) throws DataAccessException {
        Result result = new Result();
        try {
            // 供货商详情
            emCProvider = emCProviderDAO.showEmCProviderDetail(emCProvider);
            // result.setResult("获取供货商详情成功!");
            // 已添加用户
            EmCUser emCUser = new EmCUser();
            emCUser.setExtendCode(emCProvider.getProviderCode());
            emCUser.setUserType(1);
            List<EmCUser> userList = userdao.searchEmCUserList(emCUser);

            result.putData("emCProvider", emCProvider);
            result.setRows(userList);

            result.setResult("获取供应商详情成功!");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "获取供应商详情失败!");
        }

        return result;
    }

    /*
     * 插入
     */
    @Override
    public Result insertProvider(EmCProvider emCProvider) throws DataAccessException {
        Result result = checkInputEmCProvider(emCProvider);
        if (!result.getCode().equals("200")) return result;
        try {
            emCProviderDAO.insertEmCProvider(emCProvider);
            dbLog.info("INSERT-->EM_C_PROVIDER 供货商编码：" + emCProvider.getProviderCode() +"  各字段值："+JSON.toJSONString(emCProvider));
            result.setResult("添加供应商成功!");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "添加供应商失败！");

        }
        return result;
    }

    /*
     * 更新
     */
    @Override
    public Result updateProvider(EmCProvider emCProvider) throws DataAccessException {
        Result result = checkInputEmCProvider(emCProvider);
        if (!result.getCode().equals("200")) return result;
        try {
            emCProviderDAO.updateEmCProvider(emCProvider);
            dbLog.info("UPDATE-->EM_C_PROVIDER 供货商编码：" + emCProvider.getProviderCode() +"  各字段值："+JSON.toJSONString(emCProvider));
            result.setResult("更新供应商成功!");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "更新供应商失败！");

        }

        return result;
    }

    /*
     * 删除
     */
    @Override
    public Result deleteEmCProvider(EmCProvider emCProvider) throws DataAccessException {
        Result result = new Result();
        try {
            emCProviderDAO.deleteEmCProvider(emCProvider);
            dbLog.info("DELETE-->EM_C_PROVIDER 供货商编码：" + emCProvider.getProviderCode());
            result.setResult("删除供应商成功！");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "删除供应商失败！");
        }

        return result;
    }

    @Override
    public Result deleteMultipleEmCProviders(String providerIdList) {
        Result result = new Result();
        try {
            emCProviderDAO.deleteMultipleEmCProviders(providerIdList);
            dbLog.info("DELETE(Multiple-删除)-->EM_C_PROVIDER 供货商编码：" + providerIdList);
            result.setResult("批量删除供应商成功！");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "批量删除供应商失败！");
        }

        return result;
    }

    /**
     * 输入参数检查
     * 
     * @Title: checkInputEmCProvider
     */
    private Result checkInputEmCProvider(EmCProvider p) {
        Result result = new Result();
        if (StringUtils.isBlank(p.getAreaCode())) {
            result.setCodeResult(Result.CODE_100, "缺少areaCode地区!");
            return result;
        }
        if (StringUtils.isBlank(p.getAddress())) {
            result.setCodeResult(Result.CODE_100, "缺少address地址!");
            return result;
        }
        if (StringUtils.isBlank(p.getZip())) {
            result.setCodeResult(Result.CODE_100, "缺少zip邮编!");
            return result;
        }
        if (StringUtils.isBlank(p.getPhone())) {
            result.setCodeResult(Result.CODE_100, "缺少phone收货联系电话!");
            return result;
        }

        p.setUpdateDate(new Date());
        return result;

    }

    /*
     * 批量审批
     */
    @Override
    public Result editMultipleEmCProvidersStatus(String providerIdList) throws DataAccessException {
        Result result = new Result();
        try {
            emCProviderDAO.editMultipleEmCProvidersStatus(providerIdList);
            dbLog.info("UPDATE(Multiple-审核通过)-->EM_C_PROVIDER 供货商编码：" + providerIdList);
            result.setResult("批量审批供应商成功！");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "批量审批供应商失败！");
        }
        return result;
    }

	/**
	  * <p>Title: batchUpdateProvider</p>
	  * <p>Description: 批量修改查询数据的开始日期</p>
	  */
	@Override
	public Result batchUpdateProvider(EmCProvider emCProvider) throws DataAccessException {
		Result ret = new Result();
        if (CollectionUtils.isEmpty(emCProvider.getList())) {
            ret.setCodeResult(Result.CODE_100, "请选择要操作的行!");
            return ret;
        }
        try {
            for (EmCProvider e : emCProvider.getList()) {
                e.setUpdateDate(new Date());
                emCProviderDAO.updateEmCProvider(e);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException("更新商品数据失败");
        }
        return ret;
	}
}
